<!doctype html>

<head>
    <meta charset="utf-8" />
</head>

<body>
    <div>可打开浏览器控制台查看结果</div>
    <!-- <script src="https://cdn.bootcss.com/jquery/2.2.0/jquery.min.js"></script> -->
    <script src="./jquery.min.js"></script>
    <script src="./md5.js"></script>
    <script type="text/javascript">
        var appid = '20180718000186694';
        var key = '6hlRV5ytiy0S_8QKQRMJ';
        var salt = 1435660288;//(new Date).getTime();
        var query = 'apple';
        // 多个query可以用\n连接  如 query='apple\norange\nbanana\npear'
        var from = 'en';
        var to = 'zh';
        var str1 = appid + query + salt + key;
        var sign = MD5(str1);
        function testcode(){
            var tjson={"from":"en","to":"zh","trans_result":[{"src":"apple","dst":"\u82f9\u679c"}]};
            alert(( tjson.trans_result[0].dst));
            
        }

        function fanyi() {
            var intxt = document.getElementById("txt01").value;
            var inmd5 = appid + intxt + salt + key;
            var outmd5 = MD5(inmd5);
            var httpadrs = "https://api.fanyi.baidu.com/api/trans/vip/translate?q=" + intxt + "&from=en&to=zh&appid=20180718000186694&salt=1435660288&sign=" + outmd5;

            $.ajax({
                url: httpadrs,
                type: 'GET',
                dataType: 'JSONP',
                success: function (data) {
                    //提取json中的汉语翻译部分
                   document.getElementById("txt02").value=data.trans_result[0].dst;
                    
                }
            });
        }
    </script>
    <div>
        <input type="button" value="click" onclick="fanyi()">
        <br>
        <input id="txt01" type="text">
        <input id="txt02" type="text">
        <br>
        <input type="button" onclick="testcode()">
    </div>
</body>